package com.xjrsoft.module.customerTwo.ycode.t_ycode_class.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.xjrsoft.common.utils.DataTransUtil;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.core.tool.utils.CollectionUtil;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_class.dto.TYcodeClassDto;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_class.dto.SaveTYcodeClassFormDataDto;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_class.vo.*;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_class.dto.TYcodeClassListDto;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.FlowableUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_class.entity.TYcodeClass;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_class.vo.TYcodeClassVo;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_class.service.ITYcodeClassService;

/**
 *  控制器
 *
 * @author dongxinhang
 * @since 2024-07-17
 */
@RestController
@AllArgsConstructor
@RequestMapping("/t_ycode_class")
@Api(value = "分类管理", tags = "分类管理接口")
public class TYcodeClassController {


	private final ITYcodeClassService tYcodeClassService;


	/**
	 * 详情 
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入tYcodeClass")
	@SaCheckPermission(value = {"t_ycode_class:view", "t_ycode_class:modify"}, mode = SaMode.OR)
	public Response<TYcodeClassVo> getFormData(@PathVariable String id) {
		// 主表数据
		TYcodeClass tYcodeClass = tYcodeClassService.getById(id);
		TYcodeClassVo tYcodeClassVo = BeanUtil.copy(tYcodeClass, TYcodeClassVo.class);
		return Response.ok(tYcodeClassVo);
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入tYcodeClass")
	public Response<PageOutput<TYcodeClassListVo>> getPageList(TYcodeClassListDto listDto) {
		IPage<TYcodeClass> page = tYcodeClassService.getPageList(listDto);
		List<TYcodeClassListVo> records = BeanUtil.copyList(page.getRecords(), TYcodeClassListVo.class);
			// 转换列表数据
		DataTransUtil.transListShowData(records);
			// 工作流数据处理
		FlowableUtil.systemFormList(records, SecureUtil.getModuleId());
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 自定义分页
	 */
	@GetMapping("/getTreeList")
	@ApiOperation(value = "分类管理查询，返回树结构", notes = "传入tYcodeClass")
	public Response<List<TYcodeClassListTreeVo>> getTreeList(TYcodeClassListDto listDto) {
		List<TYcodeClass> tempList = tYcodeClassService.getList(listDto);
		// 转换列表数据
		DataTransUtil.transListShowData(tempList);
		List<TYcodeClassListTreeVo> list = BeanUtil.copyList(tempList,TYcodeClassListTreeVo.class);
		if(CollectionUtil.isNotEmpty(list)){
			List<TYcodeClassListTreeVo> result = list.stream().filter(t -> StringUtils.isBlank(t.getParentCode())).map(
					m -> {
						m.setChildren(getChildren(m, list));
						return m;
					}
			).collect(Collectors.toList());
			return Response.ok(result);
		}
		return Response.ok();
	}

	private List<TYcodeClassListTreeVo> getChildren(TYcodeClassListTreeVo vo,List<TYcodeClassListTreeVo> all){
		List<TYcodeClassListTreeVo> children = all.stream().filter(t -> {
			return Objects.equals(t.getParentCode(), vo.getCompleteCode());
		}).map(
				m -> {
					m.setChildren(getChildren(m, all));
					return m;
				}
		).collect(Collectors.toList());
		children.sort((a,b)->a.getCompleteCode().compareTo(b.getCompleteCode()));
		return children;
	}

	/**
	 * 新增 
	 */
	@PostMapping
	@ApiOperation(value = "新增", notes = "传入tYcodeClass")
	@SaCheckPermission("t_ycode_class:add")
	public Response save(@RequestBody SaveTYcodeClassFormDataDto formDto) {
		TYcodeClassDto tYcodeClassDto = formDto.getTYcodeClassDto();
		TYcodeClass tYcodeClass = BeanUtil.copy(tYcodeClassDto, TYcodeClass.class);
		String dataId = tYcodeClassService.addTYcodeClass(tYcodeClass);
		return dataId == null ? Response.notOk() : Response.ok(dataId);
	}

	/**
	 * 修改 
	 */
	@PutMapping("/{id}")
	@ApiOperation(value = "修改", notes = "传入tYcodeClass")
	@SaCheckPermission("t_ycode_class:modify")
	public Response update(@PathVariable String id, @RequestBody SaveTYcodeClassFormDataDto formDto) {
		TYcodeClassDto tYcodeClassDto = formDto.getTYcodeClassDto();
		TYcodeClass tYcodeClass = BeanUtil.copy(tYcodeClassDto, TYcodeClass.class);
		return Response.status(tYcodeClassService.updateTYcodeClass(id, tYcodeClass));
	}



	/**
	 * 删除 
	 */
	@DeleteMapping("/{ids}")
	@ApiOperation(value = "删除", notes = "传入ids")
	@SaCheckPermission(value = {"t_ycode_class:batchDelete", "t_ycode_class:delete"}, mode = SaMode.OR)
	public Response remove(@ApiParam(value = "主键集合", required = true) @PathVariable String ids) {
		return Response.status(tYcodeClassService.delete(ids));
	}

	/**
	 * 获取类目树
	 */
	@GetMapping("/getClassificationTree")
	@ApiOperation(value = "获取类目树", notes = "id")
	public Response getClassificationTree() {
		List<TYcodeClassTreeVo> list = tYcodeClassService.selectYcodeClassForTree();
		if(CollectionUtil.isNotEmpty(list)){
			List<TYcodeClassTreeVo> result = list.stream().filter(t -> StringUtils.isBlank(t.getParent())).map(
					m -> {
						m.setChildren(getChildren(m, list));
						return m;
					}
			).collect(Collectors.toList());
			return Response.ok(result);
		}
		return Response.ok();
	}

	private List<TYcodeClassTreeVo> getChildren(TYcodeClassTreeVo vo,List<TYcodeClassTreeVo> all){
		List<TYcodeClassTreeVo> children = all.stream().filter(t -> {
			return Objects.equals(t.getParent(), vo.getValue());
		}).map(
			m -> {
				m.setChildren(getChildren(m, all));
				return m;
			}
		).collect(Collectors.toList());
		return children;
	}

}
